<template>
	<view class="container">
		<view class="tui-searchbox">
			<view class="tui-search-input">
				<uni-icons type="search" :size='13' color='#333'></uni-icons>
				<input confirm-type="search" placeholder="大家都在搜" :focus="true" auto-focus placeholder-class="tui-input-plholder"
				 class="tui-input" v-model.trim="key" @input="inputKey"/>
				<uni-icons type="clear" :size='13' color='#bcbcbc' @tap="cleanKey" v-show="key"></uni-icons>
			</view>
			<view class="tui-cancle" @tap="back">取消</view>
		</view>

		<view class="tui-search-history" v-show="history.length>0 && !key">
			<view class="tui-history-header">
				<view class="tui-search-title">搜索历史</view>
				<tui-icon name="delete" :size='14' color='#333' @tap="openActionSheet" class="tui-icon-delete"></tui-icon>
			</view>
			<view class="tui-history-content">
				<block v-for="(item,index) in history" :key="index">
					<tui-tag style="margin:0 24rpx 24rpx 0;" type="gray" shape="circle" @click="search(item)">{{item}}</tui-tag>
				</block>
			</view>
		</view>
		<view v-show="key">
			<view class="tui-header">
				<view class="tui-header-left tui-noboredr" @click="search(key)">搜索 “{{key}}”</view>
			</view>
			<view class="tui-result-box">
				<block v-for="(item,index) in searchList" :key="index">
					<view class="tui-result-item" hover-class="tui-opcity" :hover-stay-time="150" @click="search(item.showKey)">
						<rich-text :nodes="item.showKey"></rich-text>
					</view>
				</block>
			</view>
		</view>
		
		<view class="tui-search-hot">
			<view class="tui-hot-header">
				<view class="tui-search-title">大家正在搜</view>
			</view>
			<view class="tui-hot-content">
				<block v-for="(item,index) in hot" :key="index">
					<tui-tag style="margin:0 24rpx 24rpx 0;" type="gray" shape="circle" @click="search(item)">{{item}}</tui-tag>
				</block>
			</view>
		</view>
		
		<tui-actionsheet :show="showActionSheet" :tips="tips" @click="itemClick" @cancel="closeActionSheet"></tui-actionsheet>
	</view>

</template>

<script>
	const util = require("@/static/util.js")
	import uniIcons from '@/components/uni-ui/uni-icon/uni-icon'
	import tuiIcon from '@/components/thor-ui/tui-icon/tui-icon'
	import tuiTag from '@/components/thor-ui/tui-tag/tui-tag'
	import tuiActionsheet from '@/components/thor-ui/tui-actionsheet/tui-actionsheet'
	export default {
		components: {uniIcons,tuiIcon,tuiTag,tuiActionsheet},
		data() {
			return {
				prePage:"",
				history: JSON.parse( uni.getStorageSync('search_history_01') || '[]'),
				key: "",
				showActionSheet: false,
				tips: "确认清空搜索历史吗？",
				searchList:[],
				hot: [
					"德利赫特",
					"托雷斯",
					"早安D站",
					"D站观点",
					"德利赫特1"
				],
			}
		},
		onLoad(option){
			this.prePage = option.page;
		},
		methods: {
			// 取消 返回
			back: function() {
				if(this.prePage == "main") {
					uni.switchTab({ url: "/pages/main/main"});
				}else{
					uni.navigateBack();
				}
			},
			// 清空输入框
			cleanKey: function() {
				this.key = ''
			},
			// 确认清空历史
			closeActionSheet: function() {
				this.showActionSheet = false;
			},
			// 确认清空历史
			openActionSheet: function() {
				this.showActionSheet = true;
			},
			// 清空历史
			itemClick: function(e) {
				let index = e.index;
				if (index == 0) {
					this.showActionSheet = false;
					this.history = [];
					uni.setStorageSync('search_history_01',"");
				}
			},
			//监听输入
			inputKey: function(e) {
				this.key = util.trim(e.detail.value);
				if (!this.key) {
					this.searchList = [];
					return;
				}
				//根据关键词查找
				let arr = []
				//实际开发中，根据搜索返回结果集，此处只是做展示提示搜索哪些文字
				let searchResult = ["thorui","2019退役球星"];
				searchResult.forEach((item) => {
					arr.push({
						key: item,
						showKey: util.replaceAll(item, this.key, `<label style="color:#E41F19">${this.key}</label>`)
					})
				})
				this.searchList = arr
			},
			search(key){
				if(this.history.indexOf(key) ==-1) this.history.unshift(key);
				uni.setStorageSync('search_history_01', JSON.stringify(this.history) );
				
				console.log(this.history)
				console.log(key)
			}		
		}
	}
</script>

<style>
	page {
		color: #333;
		background: #fff;
	}
	.container {
		padding: 0 30rpx 30rpx 30rpx;
		box-sizing: border-box;
		width: 100%;
	}

	.tui-searchbox {
		padding: 30rpx 0;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.tui-search-input {
		width: 100%;
		height: 66rpx;
		border-radius: 35rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		background: #f2f2f2;
		display: flex;
		align-items: center;
		flex-wrap: nowrap;
	}

	.tui-input {
		flex: 1;
		color: #333;
		padding: 0 16rpx;
		font-size: 28rpx;
	}

	.tui-input-plholder {
		font-size: 28rpx;
		color: #b2b2b2;
	}

	.tui-cancle {
		color: #888;
		font-size: 28rpx;
		padding-left: 30rpx;
		flex-shrink: 0;
	}

	.tui-history-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx 0;
	}
	.tui-history-content{
		display: flex;
		align-items: center;
		flex-wrap: wrap;
	}

	.tui-icon-delete {
		padding: 10rpx;
	}

	.tui-search-title {
		font-size: 28rpx;
		font-weight: bold;
	}

	.tui-hot-header {
		padding: 30rpx 0;
	}
	
	.tui-header {
		padding: 26rpx 0;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	
	.tui-header-left {
		font-size: 30rpx;
		color: #222;
		border-left: 4rpx solid #EB0909;
		padding-left: 10rpx;
		word-break: break-all;
	}
	
	.tui-noboredr {
		border-left: 0 !important;
	}

	.tui-result-box {
		font-size: 28rpx;
	}
	
	.tui-result-item {
		line-height: 28rpx;
		padding: 28rpx 0;
	}
</style>